package org.example;

// 基础组件接口
interface Coffee {
    double cost();
    String description();
}

// 具体组件
class BasicCoffee implements Coffee {
    @Override
    public double cost() {
        return 5.0;
    }

    @Override
    public String description() {
        return "Basic Coffee";
    }
}

// 装饰者基类
abstract class CoffeeDecorator implements Coffee {
    protected Coffee coffee;

    public CoffeeDecorator(Coffee coffee) {
        this.coffee = coffee;
    }

    @Override
    public double cost() {
        return coffee.cost();
    }

    @Override
    public String description() {
        return coffee.description();
    }
}

// 具体装饰者
class MilkDecorator extends CoffeeDecorator {
    public MilkDecorator(Coffee coffee) {
        super(coffee);
    }

    @Override
    public double cost() {
        return super.cost() + 2.0;
    }

    @Override
    public String description() {
        return super.description() + ", Milk";
    }
}

class SugarDecorator extends CoffeeDecorator {
    public SugarDecorator(Coffee coffee) {
        super(coffee);
    }

    @Override
    public double cost() {
        return super.cost() + 1.0;
    }

    @Override
    public String description() {
        return super.description() + ", Sugar";
    }
}

// 测试装饰者模式
public class DecoratorPattern {
    public static void main(String[] args) {
        Coffee basicCoffee = new BasicCoffee();
        System.out.println(basicCoffee.description() + " Cost: $" + basicCoffee.cost());
        // 输出 "Basic Coffee Cost: $5.0"

        Coffee milkCoffee = new MilkDecorator(basicCoffee);
        System.out.println(milkCoffee.description() + " Cost: $" + milkCoffee.cost());
        // 输出 "Basic Coffee, Milk Cost: $7.0"

        Coffee sugarMilkCoffee = new SugarDecorator(milkCoffee);
        System.out.println(sugarMilkCoffee.description() + " Cost: $" + sugarMilkCoffee.cost());
        // 输出 "Basic Coffee, Milk, Sugar Cost: $8.0"
    }
}